//
//  YXNavigationController.m
//  Ameson
//
//  Created by xmdfst on 2017/1/4.
//  Copyright © 2017年 dofast. All rights reserved.
//

#import "YXNavigationController.h"

@interface YXNavigationController ()

@end

@implementation YXNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [YXNavigationController setNaviBarStyle:self.navigationBar];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.viewControllers.count > 0) {
        [viewController setHidesBottomBarWhenPushed:YES];
    }
    
    
    [super pushViewController:viewController animated:animated];
    
    //替换掉leftBarButtonItem
    if (viewController.navigationItem.leftBarButtonItem== nil && [self.viewControllers count] > 1) {
        viewController.navigationItem.leftBarButtonItem =[self customLeftBackButton];
    }
    
}
#pragma mark - 自定义返回按钮图片
-(UIBarButtonItem*)customLeftBackButton{
    
    UIImage *image = [UIImage imageNamed:@"fanhui"];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    backButton.frame = CGRectMake(0, 0, image.size.width, image.size.height);
    
    [backButton setBackgroundImage:image
                          forState:UIControlStateNormal];
    
    [backButton addTarget:self
                   action:@selector(popself)
         forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    
    return backItem;
}

#pragma mark - 返回按钮事件(pop)
-(void)popself
{
    [self popViewControllerAnimated:YES];
}

+(void)setNaviBarStyle:(UINavigationBar*)naviBar
{
    [naviBar setBackgroundImage:[UIImage imageWithColor:[DFColor mainColor]] forBarMetrics:UIBarMetricsDefault];
    [naviBar setTintColor:[UIColor whiteColor]];
    naviBar.translucent = NO;
    [naviBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[DFColor naviTitleColor],NSFontAttributeName:FONT_BOLD(16)}];
    [naviBar setShadowImage:[UIImage imageWithColor:[DFColor dashLight]]];
    
}
@end
